package com.zs.zcerp.remark.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zs.zcerp.remark.entity.LayUIDto;
import com.zs.zcerp.remark.entity.TgPpsM;
import com.zs.zcerp.remark.entity.TgPpsScore;
import com.zs.zcerp.remark.service.ITgPpsMService;
import com.zs.zcerp.remark.service.ITgPpsScoreService;
import com.zs.zcerp.system.entity.User;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * (TgPpsScore)表控制层
 *
 * @author makejava
 * @since 2021-04-15 07:58:44
 */
@Slf4j
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("remark")
public class TgPpsScoreController {
    /**
     * 服务对象
     */
    @Autowired
    private ITgPpsScoreService tgPpsScoreService;

    @Autowired
    private ITgPpsMService tgPpsMService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public TgPpsScore selectOne(Integer id) {
        return null;
    }

    /**
     * 通过提案编号查询单条数据,评分页用
     *
     * @param ppsNo 提案编号
     * @return 单条数据
     */
    @GetMapping("pps/{ppsNo}")
    public LayUIDto selectOne(@PathVariable("ppsNo") String ppsNo) {
        if (ppsNo == null){
            return null;
        }
        User user =(User) SecurityUtils.getSubject().getPrincipal();
        TgPpsM tgPpsM = tgPpsMService.findByPpsNo(ppsNo);
        LayUIDto layUIDto = new LayUIDto();
        layUIDto.setCode(0);
        TgPpsScore tgPpsScore = tgPpsScoreService.findByPpsNo(ppsNo, user.getUsername());
        int count = 0;
        if (tgPpsScore != null) {
            count = 1;
            tgPpsM.setBenefit(tgPpsScore.getBenefit());
            tgPpsM.setTechnical(tgPpsScore.getTechnical());
            tgPpsM.setCreativity(tgPpsScore.getCreativity());
            tgPpsM.setRemark(tgPpsScore.getRemark());
        }
        layUIDto.setCount(count);
        layUIDto.setMsg("success");
        layUIDto.setData(tgPpsM);
        return layUIDto;
    }

    @PostMapping( "save")
    public String saveRemark(@RequestBody String data){
        User user =(User) SecurityUtils.getSubject().getPrincipal();
        JSONObject req = JSON.parseObject(data);
        Integer count = tgPpsScoreService.checkIfExist(req.getString("ppsNo"), user.getUsername());
        if(count == 0){
            TgPpsScore tgPpsScore = new TgPpsScore();
            tgPpsScore.setPpsNo(req.getString("ppsNo"));
            tgPpsScore.setScoreUser(user.getUsername());
            tgPpsScore.setBenefit(req.getInteger("benefit_score"));
            tgPpsScore.setCreativity(req.getInteger("creativity_score"));
            tgPpsScore.setCreaTime(new Date());
            tgPpsScore.setOrgId(req.getInteger("orgId"));
            tgPpsScore.setRemark(req.getString("remark"));
            tgPpsScore.setTechnical(req.getInteger("technical_score"));
            try {
                tgPpsScoreService.add(tgPpsScore);
                return "保存评论成功";
            } catch (Exception e) {
                e.printStackTrace();
                return "系统临时故障,评论失败,请稍后再试";
            }
        } else {
            return "你已评论过,不能重复评论";
        }
    }

}